import time

from django.db import models

# Create your models here.
from user_app.models import User


class Article(models.Model):

	id = models.AutoField(primary_key=True)
	title = models.CharField(max_length=128)
	text = models.TextField()
	content_text = models.TextField(default='')
	head_avatar = models.CharField(default='', max_length=256)
	add_time = models.IntegerField(default=time.time())
	change_time = models.IntegerField(default=time.time())
	is_delete = models.IntegerField(default=0)
	mode = models.CharField(max_length=12, default='1')
	tags = models.CharField(max_length=128, default="")
	view_no = models.IntegerField(default=0, verbose_name='查看数')
	agree_no = models.IntegerField(default=0, verbose_name='点赞数')
	comment_no = models.IntegerField(default=0, verbose_name='评论数')
	star_no = models.IntegerField(default=0, verbose_name='收藏数')
	u_id = models.ForeignKey(User, on_delete='SET_DEFAULT', default=0, db_column='u_id')
	is_public = models.IntegerField(default=1, verbose_name='是否公开')  # 默认为公开
	is_check = models.IntegerField(default=0, verbose_name='是否检查通过允许发表')  # 默认否
	word = models.CharField(default='', max_length=256)
	recommend = models.IntegerField(default=0, verbose_name='推荐等级')
	is_recommend = models.IntegerField(default=0, verbose_name="站长推荐")

	class Meta:
		db_table = "article_db"
		managed = False
		verbose_name = '文章'
		verbose_name_plural = verbose_name

	def __str__(self):
		return self.title

	def delete(self):
		self.is_delete = 1
		super().save()


class Comment(models.Model):
	"""
	id	评论的id，自增值，每个评论都对应一个唯一的commentId
	article_id	评论所对应的新闻的id
	content	评论的内容
	u_id	发出该评论用户的id
	p_id	指向父评论的id,如果不是对评论的回复,那么该值为null
	add_time	评论产生日期
	"""
	id = models.AutoField(primary_key=True)
	from_uid = models.ForeignKey(User, on_delete='SET_DEFAULT', default='', db_column='from_uid', verbose_name='评论用户')
	article_id = models.ForeignKey(Article, on_delete='SET_DEFAULT', default='', db_column='article_id', verbose_name='评论文章')
	content = models.TextField(verbose_name='评论内容')
	level = models.IntegerField(default=1, verbose_name='评论类型')  # 评论类型 1为回复文章 2为回复评论
	content_text = models.TextField(default='')
	add_time = models.IntegerField(default=time.time(), verbose_name="评论时间")
	is_delete = models.IntegerField(default=0, verbose_name='是否删除')

	class Meta:
		db_table = "comment_db"


class CommentRelation(models.Model):

	id = models.AutoField(primary_key=True)
	parent_id = models.ForeignKey(Comment, on_delete='SET_DEFAULT', default='', db_column='parent_id', related_name='parent')
	child_id = models.ForeignKey(Comment, on_delete='SET_DEFAULT', default='', db_column='child_id', related_name='child_id')

	class Meta:
		db_table = "comment_relation_db"


class Agree(models.Model):
	id = models.AutoField(primary_key=True)
	from_uid = models.ForeignKey(User, on_delete='SET_DEFAULT', default='', db_column='from_uid', verbose_name='点赞用户')
	aim_id = models.IntegerField(default=1, verbose_name='点赞id')
	aim_type = models.CharField(max_length=12, default="article", verbose_name="点赞类型")
	is_delete = models.IntegerField(default=0, verbose_name='是否点赞')

	class Meta:
		db_table = "agree_db"


class Message(models.Model):
	"""消息通知系统  可用于系统通知、评论通知、回复通知等"""
	id = models.AutoField(primary_key=True)
	from_id = models.IntegerField(default=0, verbose_name='来自谁')  # 默认来自系统 id为0
	to_id = models.IntegerField(default=0, verbose_name='发给谁')
	need_reply = models.IntegerField(default=0, verbose_name='是否需要回复')
	link = models.CharField(default="", max_length=1024, verbose_name='消息关联文章')
	link_com = models.IntegerField(default=0, verbose_name='消息关联评论')
	info = models.CharField(default="", max_length=1024, verbose_name='消息内容')
	message_type = models.CharField(max_length=12, default="系统消息", verbose_name="消息类型")
	is_check = models.IntegerField(default=0, verbose_name='是否目标人已查看')
	add_time = models.IntegerField(default=time.time())

	class Meta:
		db_table = "message_db"